/**导出excel
 * by fuzhou huilin corp.ltd
 * 832 2016年12月14日
 */
package com.fzrskj.cloud.free.core.utils;

import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.core.tool.CDateTimeTool;
import com.fzrskj.cloud.free.user.bean.UserInvoice;
import com.fzrskj.cloud.free.user.utils.UserInvoiceUtil;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import net.sf.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class JxlExpExcelUtils {
	public String exportExcel(String[] array,String excelPath){
		List<UserInvoice> list = new ArrayList<UserInvoice>();
		UserInvoiceUtil userInvoiceUtil = new UserInvoiceUtil();
		for(int i=0;i<array.length;i++){
			list.add(userInvoiceUtil.queryUIById(CStrTool.strToIntDef(array[i], 0)));
		}
		String excelFile = "";
		String fileName = CDateTimeTool.datetimeToInt(new Date())+".xls";
		if((System.getProperty ("os.name")).indexOf("Win")>-1 ||(System.getProperty ("os.name")).indexOf("win")>-1){
			excelFile = excelPath + "\\" + fileName;
		}else{
			excelFile = excelPath + "/" + fileName;
		}
		File file = new File(excelFile);
		String[] title = {"发票类型","发票抬头","纳税人识别号","开户行","开户账号","注册地址","注册银行联系电话","一般纳税证明","发票金额","发票内容","收件方地址","收件公司名","邮政编码","收件人","联系电话","备注","发票获取途径"};
		String result="";
		JSONObject obj_result = new JSONObject();
		try {
			//在路径下创建文件
			file.createNewFile();
			//创作工作簿
			WritableWorkbook workbook = Workbook.createWorkbook(file);
			//创建sheet页
			WritableSheet sheet = workbook.createSheet("sheel", 0);
			Label label = null;
			int a = 1;
			for (int i = 0; i < title.length; i++) {
				sheet.setColumnView(i, 30); // 设置列的宽度

				label = new Label(i,0,title[i]);
				sheet.addCell(label);
			}
			for(int i = 0;i< list.size();i++){
				int line = 0;
				sheet.setRowView(a, 500); // 设置行的高度
				if(list.get(i).getType().equals("0")){
					label = new Label(line,a,"增值税普通发票(小规模纳税人)");
					line = line+1;
				}else{
					label = new Label(line,a,"增值税专用发票(一般纳税人)");
					line = line+1;
				}
				sheet.addCell(label);
				label = new Label(line,a,list.get(i).getHeader());
				line = line+1;
				sheet.addCell(label);
				label = new Label(line,a,list.get(i).getIdentityId());
				line = line+1;
				sheet.addCell(label);
				label = new Label(line,a,list.get(i).getBankName());
				line = line+1;
				sheet.addCell(label);
				label = new Label(line,a,list.get(i).getOpenAccount());
				line = line+1;
				sheet.addCell(label);
				label = new Label(line,a,list.get(i).getRegisterAddress());
				line = line+1;
				sheet.addCell(label);
				label = new Label(line,a,list.get(i).getContactNumber());
				line = line+1;
				sheet.addCell(label);
				label = new Label(line,a,list.get(i).getMaterialUrl());
				line = line+1;
				sheet.addCell(label);
				label = new Label(line,a,String.valueOf(list.get(i).getPrice()));
				line = line+1;
				sheet.addCell(label);
				if(list.get(i).getContent().equals("0")){
					label = new Label(line,a,"云网络服务费");
					line = line+1;
				}else{
					label = new Label(line,a,"网络服务费");
					line = line+1;
				}
				sheet.addCell(label);
				label = new Label(line,a,list.get(i).getRecipientAddress());
				line = line+1;
				sheet.addCell(label);
				label = new Label(line,a,list.get(i).getCompanyName());
				line = line+1;
				sheet.addCell(label);
				label = new Label(line,a,list.get(i).getPostcode());
				line = line+1;
				sheet.addCell(label);
				label = new Label(line,a,list.get(i).getAddressee());
				line = line+1;
				sheet.addCell(label);
				label = new Label(line,a,list.get(i).getTelephone());
				line = line+1;
				sheet.addCell(label);
				label = new Label(line,a,list.get(i).getRemark());
				line = line+1;
				sheet.addCell(label);
				if(list.get(i).getGetType().equals("express")){
					label = new Label(line,a,"快递（15元）");
					line = line+1;
				}else if(list.get(i).getGetType().equals("certifiedMail")){
					label = new Label(line,a,"挂号信（5元）");
					line = line+1;
				}else if(list.get(i).getGetType().equals("emailReceipts")){
					label = new Label(line,a,"电子收据（免费）");
					line = line+1;
				}else if(list.get(i).getGetType().equals("askFor")){
					label = new Label(line,a,"到公司自取（免费）");
					line = line+1;
				}
				sheet.addCell(label);
				a=a+1;
			}
			//写入数据
			workbook.write();
			workbook.close();
			obj_result.put("code", "1");
			obj_result.put("FileName", fileName);
			result = obj_result.toString();
		} catch (Exception e) {
			e.printStackTrace();
			obj_result.put("code", "-1");
			result = obj_result.toString();
		}
		return result.toString();
	}	

}
